package com.gg.fmallspringboot.service.impl;

import com.gg.fmallspringboot.config.WCPayConfig;
import com.gg.fmallspringboot.model.Result;
import com.gg.fmallspringboot.service.WCPayService;
import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 作者:Miki
 * 时间:2021/6/20 22:40
 */
@Service
public class WCPayServiceImpl implements WCPayService {

    @Override
    public Result unifiedorder(String orderId,Integer fee ,String notifyUrl) {
        try {
            //1.构建配置
            WCPayConfig config = new WCPayConfig();
            //2.封装请求数据
            Map<String,String> requestParams = new HashMap<String, String>();
            requestParams.put("appid",config.getAppID());
            requestParams.put("mch_id",config.getMchID());
            requestParams.put("nonce_str", WXPayUtil.generateNonceStr());
            requestParams.put("body","微信支付"); //
            requestParams.put("out_trade_no",orderId);//业务系统订单号
            requestParams.put("total_fee",fee+"");//订单金额，单位为分
            requestParams.put("spbill_create_ip","127.0.0.1");
            requestParams.put("notify_url",notifyUrl);//微信支付成功后回调方法的URL
            requestParams.put("trade_type","NATIVE");//扫码支付

            //3.将map转xml串
            String reqeuestParamsXml = WXPayUtil.generateSignedXml(requestParams, config.getKey());

            //4.发请求
            WXPayRequest request = new WXPayRequest(config);
            String resultXml = request.requestWithoutCert("/pay/unifiedorder", UUID.randomUUID().toString(), reqeuestParamsXml, false);
            System.out.println(resultXml);

            //5.将resultXml转map集合
            final Map<String, String> resultMap = WXPayUtil.xmlToMap(resultXml);

            return new Result(0,"下单成功",resultMap.get("code_url"));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(0,"下单失败");
        }

    }

    @Override
    public Result orderquery(String orderId) {
        try {
            //1.构建配置
            WCPayConfig config = new WCPayConfig();
            //2.封装请求数据
            Map<String,String> requestParams = new HashMap<String, String>();
            requestParams.put("appid",config.getAppID());
            requestParams.put("mch_id",config.getMchID());
            requestParams.put("out_trade_no", orderId);
            requestParams.put("nonce_str",WXPayUtil.generateNonceStr());
            //3.将map转xml串
            String reqeuestParamsXml = WXPayUtil.generateSignedXml(requestParams, config.getKey());

            //4.发请求
            WXPayRequest request = new WXPayRequest(config);
            String resultXml = request.requestWithoutCert("/pay/orderquery", UUID.randomUUID().toString(), reqeuestParamsXml, false);
            System.out.println(resultXml);

            //5.将resultXml转map集合
            final Map<String, String> resultMap = WXPayUtil.xmlToMap(resultXml);
            return new Result(0,"查询成功",resultMap.get("trade_state"));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(0,"查询失败");
        }

    }

    @Override
    public Result closeorder(String orderId) {
        try {
            //1.构建配置
            WCPayConfig config = new WCPayConfig();
            //2.封装请求数据
            Map<String,String> requestParams = new HashMap<String, String>();
            requestParams.put("appid",config.getAppID());
            requestParams.put("mch_id",config.getMchID());
            requestParams.put("out_trade_no", orderId);
            requestParams.put("nonce_str",WXPayUtil.generateNonceStr());
            //3.将map转xml串
            String reqeuestParamsXml = WXPayUtil.generateSignedXml(requestParams, config.getKey());

            //4.发请求
            WXPayRequest request = new WXPayRequest(config);
            String resultXml = request.requestWithoutCert("/pay/closeorder", UUID.randomUUID().toString(), reqeuestParamsXml, false);
            System.out.println(resultXml);

            //5.将resultXml转map集合
            final Map<String, String> resultMap = WXPayUtil.xmlToMap(resultXml);

            return new Result(0,"关闭成功",resultMap.get("result_code"));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(0,"关闭失败");
        }
    }
}
